package com.tzr.web;

import com.tzr.bean.User;
import com.tzr.service.UserDaoService;
import com.tzr.service.UserDaoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    public void destroy() {
        super.destroy();
    }

    @Override
    public void init() throws ServletException {
        super.init();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收参数
        req.setCharacterEncoding("utf-8");
        String uname=req.getParameter("uname");
        String upass=req.getParameter("upass");
        //2.获取service方法
        UserDaoService userDaoService=new UserDaoServiceImpl();
        User u = userDaoService.login(uname,upass);
        System.out.println(u);

        // 3.跳转页面
        if(uname.equals(u.getUname())&&upass.equals(u.getUpassword())){
            HttpSession session= req.getSession();
            session.setAttribute("login",uname);
            req.setAttribute("name",u.getUname());
            req.getRequestDispatcher("show.jsp").forward(req,resp);
        }else{
            System.out.println("denglucuowu");
            req.getRequestDispatcher("error.jsp").forward(req,resp);

        }

    }
}
